<?php
/**
 * Description of 
 *
 * @author Marco Mounzer
 * @email m.mounzer@gmail.com
 * @copyright 2013
 */
?>
<div style="border: 0px solid grey; width: 342px; padding: 0px">
    <img id="img_predefinita" src="<?php echo $img; ?>" style="padding: 2px; max-width: 332px;">

    <?php
    $sql = 'SELECT * FROM photo WHERE car_id = ' . Database::GetIstance()->quote($obj->Id);
    $data = Database::GetIstance()->select($sql);
    if (count($data) > 0) {
        ?>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#gallery a').each(function(){
                    var $img = $(this).find('img').eq(0);
                                                                                                                                                                                                                                                                   
                    $img.attr('src', $(this).attr('href'));
                                                                                                                    
                    $(this).click(function() {
                        var image = $('#img_predefinita');
                        var new_src = $(this).attr('href');
                        image.fadeOut('fast', function () {
                            image.attr('src', new_src);
                            image.fadeIn('fast');
                        });
                    
                        return false;
                    });
                });
            });
                                                                                                                                                
            function deleteImg(id, car_id) {
                if(!confirm('Sicuro di voler cancelalre la foto?'))
                    return;
                                                                                                                                        
                $.ajax({
                    type: "POST",
                    url: "photo.php",
                    data: {
                        id: id,
                        _delete: true,
                        car_id: car_id
                    }
                }).done(function() {
                    $('#a' + id).parent().remove();
                });
            }
                                                                                                                                
            function imgPredefinita(id, photo_id) {
                if(!confirm('Sicuro di voler impostare questa foto come predefinita?'))
                    return;
                                                                                                                                        
                $.ajax({
                    type: "POST",
                    url: "car.php",
                    data: {
                        id: id,
                        _save: true,
                        photo_id: photo_id,
                        solo_photo: true
                    }
                }).done(function() {
                    $('#img_predefinita').attr('src', $('#a' + photo_id).attr('href'));
                });
            }
        </script>

        <div id="gallery" class="outer">
            <?php
            $i = 0;
            foreach ($data as $v) {
                ?>
                <div class="inner">
                    <a id="a<?php echo $v->id; ?>" href="/images/cars/<?php echo $v->id . '.' . $v->extension; ?>" ord="<?php echo $i; ?>">
                        <img src="/images/loader_dg.gif" alt="" title="" ord="<?php echo $i; ?>"/>
                    </a>
                    <span onclick="deleteImg(<?php echo $v->id; ?>, <?php echo $obj->Id; ?>)" style="text-decoration: underline; cursor: pointer;">del</span> -
                    <span onclick="imgPredefinita(<?php echo $obj->Id; ?>, <?php echo $v->id; ?>)" style="text-decoration: underline; cursor: pointer;">default</span>
                </div>
                <?php
                $i++;
            }
            ?>
        </div>
        <?php
    }
    ?>
</div>